using TickSharp.Core.Infrastructure.Repositories;
using TickSharp.Core.Infrastructure.Serializers;
using TickSharp.Core.Interfaces;
using TickSharp.Core.Interfaces.Repositories;
using TickSharp.Core.Model;

namespace TickSharp.DependencyResolution
{
    public static class DependencyRegistrar
    {
        public static void EnsureDependenciesRegistered()
        {
            if (Core.IoC.IsInitialized)
            {
                return;
            }

            var dependencyResolver = new DependencyResolver();            
            RegisterDependencies(dependencyResolver);
            Core.IoC.Initialize(dependencyResolver);
        }

        private static void RegisterDependencies(IDependencyResolver dependencyResolver)
        {
            RegisterSerializers(dependencyResolver);
            RegisterRepositories(dependencyResolver);
        }

        private static void RegisterSerializers(IDependencyResolver dependencyResolver)
        {
            dependencyResolver.Register<ISerializer<Client>, ClientSerializer>();
            dependencyResolver.Register<ISerializer<Entry>, EntrySerializer>();
            dependencyResolver.Register<ISerializer<Project>, ProjectSerializer>();
            dependencyResolver.Register<ISerializer<User>, UserSerializer>();
            dependencyResolver.Register<ISerializer<Task>, TaskSerializer>();
            dependencyResolver.Register<ISerializer<Project>, ProjectSerializer>();
        }

        private static void RegisterRepositories(IDependencyResolver dependencyResolver)
        {
            dependencyResolver.Register<IClientRepository, ClientRepository>();
            dependencyResolver.Register<IEntryRepository, EntryRepository>();
            dependencyResolver.Register<IProjectRepository, ProjectRepository>();
            dependencyResolver.Register<IUserRepository, UserRepository>();
            dependencyResolver.Register<ITaskRepository, TaskRepository>();
            dependencyResolver.Register<IProjectRepository, ProjectRepository>();
        }
    }
}